package com.verlet.gateway;

import com.verlet.gateway.filter.AuthorizeGatewayFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootApplication
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // @Bean
    public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
        // 直接实现GatewayFilter接口 需要配置上加入到路由
        return builder.routes().route("customer_filter_router",
                r -> r.order(0).path("/authFilter/**")
                        .filters(f -> f.filter(new AuthorizeGatewayFilter(stringRedisTemplate))
                                .addResponseHeader("X-Response-Default-Foo", "Default-Bar"))
                        .uri("lb://web")).build();
    }

}
